﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using University.Workshop.Model;
using University.Workshop.Business;
using System.Web.Security;
using System.Web.Configuration;
using University.Workshop.DataAccess;
using System.Globalization;

namespace University.Workshop.WebApp
{
    public partial class WatchList : System.Web.UI.Page
    {
        Collection<WatchListItem> list;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                FormsAuthentication.RedirectToLoginPage();
            }

            User user = DataAccess.UserDataAccess.GetUserByEmail(User.Identity.Name);
            if (Session["watched"] != null)
                list = Session["watched"] as Collection<WatchListItem>;
            else
            {
                list = new Collection<WatchListItem>();
                list = WatchListDataAccess.GetList(user.Id);
            }

            Product prod = Session["prod"] as Product;
            string comment = string.Empty;

            if (prod != null)
            {
                Session.Remove("prod");
                long id = prod.ProductId;
                list = WatchListManager.AddToWatchList(id, user.Id, comment, list);
            }
            else
            {
                list = WatchListDataAccess.GetList(user.Id);
                if (list==null)
                {
                    this.MultiView1.ActiveViewIndex = 2;
                }
                
            }
            Session["watched"] = list;
            if (!IsPostBack)
            {
                dlWatchList.DataSource = list;
                dlWatchList.DataBind();
            }
        }
        

        protected void EditNote_Click1(object sender, EventArgs e)
        {
            Button b = sender as Button;
            TextBox t = new TextBox();
            t=sender as TextBox;
            User user = DataAccess.UserDataAccess.GetUserByEmail(User.Identity.Name);
            list= WatchListManager.AddComment(Convert.ToInt64(b.CommandArgument), user.Id, t.Text,list);
            dlWatchList.DataSource = list;
            dlWatchList.DataBind();
        }

        protected void Delete_Click1(object sender, EventArgs e)
        {
            Button b = sender as Button;
            User user = DataAccess.UserDataAccess.GetUserByEmail(User.Identity.Name);
            list=WatchListManager.RemoveItem(user.Id, Convert.ToInt64(b.CommandArgument),list);
            if (list.Count < 1)
            {
                this.MultiView1.ActiveViewIndex = 2;
            }
            else
            {
                dlWatchList.DataSource = list;
                dlWatchList.DataBind();
            }
        }
        
        protected void Comment_TextChanged(object sender, EventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Session["watched"] = list;
            Response.Redirect("ViewProduct.aspx");
        }

        protected void compareButton_Click(object sender, EventArgs e)
        {
            Collection<long> prodIds = new Collection<long>();
            int i = 0;
            foreach (DataListItem item in dlWatchList.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("chkBox");
                bool checkedValue = chk.Checked;
                if (checkedValue)
                {
                    prodIds.Add(list[i].RelatedProduct.ProductId);
                }
                i++;
            }
            if (prodIds.Count < 2)
                LblInfoComparation.Visible = true;
            else
            {
                Session["compListIds"] = prodIds;
                Response.Redirect("CompareProducts.aspx");
            }
        }

        protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSelectAll.Checked)
            {
                foreach (DataListItem item in dlWatchList.Items)
                {
                    CheckBox chk = (CheckBox) item.FindControl("chkBox");
                    chk.Checked = true;
                }
            }
            else
            {
                foreach (DataListItem item in dlWatchList.Items)
                {
                    CheckBox chk = (CheckBox)item.FindControl("chkBox");
                    chk.Checked = false;
                }
            }
        }

        protected void AddNote_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            User user = DataAccess.UserDataAccess.GetUserByEmail(User.Identity.Name);
            Product prod = DataAccess.ProductDataAccess.GetProductById(Convert.ToInt64(b.CommandArgument));
            this.MultiView1.ActiveViewIndex = 1;
            Label2.Text = prod.Name;
            Label3.Text = prod.ProductId.ToString();
            Collection<WatchListItem> watch = DataAccess.WatchListDataAccess.GetList(user.Id);
            foreach (var item in watch)
            {
                if (item.RelatedProduct.ProductId == prod.ProductId)
                {
                    TextBox2.Text = item.Comment;
                    break;
                }
            }
            

        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            User user = DataAccess.UserDataAccess.GetUserByEmail(User.Identity.Name);
            list=WatchListManager.AddComment(Convert.ToInt64(Label3.Text), user.Id, TextBox2.Text, list);
            this.MultiView1.ActiveViewIndex = 0;
            dlWatchList.DataSource = list;
            dlWatchList.DataBind();
            //Session["watched"] = list;
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            TextBox2.Text = null;
        }

        protected void dlWatchList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            this.MultiView1.ActiveViewIndex = 0;
        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            LinkButton b= sender as LinkButton;
            bool isInShoppingCart = false;
            Product prod = ProductDataAccess.GetProductById(long.Parse(b.CommandArgument, CultureInfo.CurrentCulture));
            Collection<Product> products = Session["carrito"] as Collection<Product>;
            if (products != null)
            {
                foreach (var item in products)
                {
                    if (item.ProductId == prod.ProductId)
                    {
                        Response.Write("<script>alert('That Product Is already in your Cart!'); </script>");
                        isInShoppingCart = true;
                        break;
                    }
                }
            }
            if (products == null)
            {
                products = new Collection<Product>();
                Session["carrito"] = products;
            }
            prod.Quantity = 1;
            Session["products"] = prod;
            if (!isInShoppingCart)
            {
                Response.Redirect("ShoppingCart.aspx");
            }
        }
    }
}